﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Core.Address;
using HslCommunication.Core.IMessage;
using HslCommunication.Core.Net;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.Melsec
{
	/// <summary>
	/// 三菱PLC通讯类，采用Qna兼容3E帧协议实现，需要在PLC侧先的以太网模块先进行配置，必须为ASCII通讯格式<br />
	/// Mitsubishi PLC communication class is implemented using Qna compatible 3E frame protocol. 
	/// The Ethernet module on the PLC side needs to be configured first. It must be ascii communication.
	/// </summary>
	/// <remarks>
	/// <inheritdoc cref="T:HslCommunication.Profinet.Melsec.MelsecMcNet" path="remarks" />
	/// </remarks>
	/// <example>
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\MelsecAscii.cs" region="Usage" title="简单的短连接使用" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\MelsecAscii.cs" region="Usage2" title="简单的长连接使用" />
	/// </example>
	// Token: 0x0200006E RID: 110
	public class MelsecMcAsciiNet : NetworkDeviceBase
	{
		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.#ctor" />
		// Token: 0x060008F5 RID: 2293 RVA: 0x000331E3 File Offset: 0x000313E3
		public MelsecMcAsciiNet()
		{
			base.WordLength = 1;
			this.LogMsgFormatBinary = false;
			base.ByteTransform = new RegularByteTransform();
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.#ctor(System.String,System.Int32)" />
		// Token: 0x060008F6 RID: 2294 RVA: 0x00033218 File Offset: 0x00031418
		public MelsecMcAsciiNet(string ipAddress, int port)
		{
			base.WordLength = 1;
			this.IpAddress = ipAddress;
			this.Port = port;
			this.LogMsgFormatBinary = false;
			base.ByteTransform = new RegularByteTransform();
		}

		/// <inheritdoc />
		// Token: 0x060008F7 RID: 2295 RVA: 0x00033266 File Offset: 0x00031466
		protected override INetMessage GetNewNetMessage()
		{
			return new MelsecQnA3EAsciiMessage();
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.Melsec.MelsecMcNet.NetworkNumber" />
		// Token: 0x17000189 RID: 393
		// (get) Token: 0x060008F8 RID: 2296 RVA: 0x0003326D File Offset: 0x0003146D
		// (set) Token: 0x060008F9 RID: 2297 RVA: 0x00033275 File Offset: 0x00031475
		public byte NetworkNumber { get; set; } = 0;

		/// <inheritdoc cref="P:HslCommunication.Profinet.Melsec.MelsecMcNet.NetworkStationNumber" />
		// Token: 0x1700018A RID: 394
		// (get) Token: 0x060008FA RID: 2298 RVA: 0x0003327E File Offset: 0x0003147E
		// (set) Token: 0x060008FB RID: 2299 RVA: 0x00033286 File Offset: 0x00031486
		public byte NetworkStationNumber { get; set; } = 0;

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.McAnalysisAddress(System.String,System.UInt16)" />
		// Token: 0x060008FC RID: 2300 RVA: 0x0003299A File Offset: 0x00030B9A
		protected virtual OperateResult<McAddressData> McAnalysisAddress(string address, ushort length)
		{
			return McAddressData.ParseMelsecFrom(address, length);
		}

		/// <inheritdoc />
		// Token: 0x060008FD RID: 2301 RVA: 0x00033290 File Offset: 0x00031490
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<McAddressData> operateResult = this.McAnalysisAddress(address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				List<byte> list = new List<byte>();
				ushort num = 0;
				while (num < length)
				{
					ushort num2 = (ushort)Math.Min((int)(length - num), 450);
					operateResult.Content.Length = num2;
					OperateResult<byte[]> operateResult2 = this.ReadAddressData(operateResult.Content);
					bool flag2 = !operateResult2.IsSuccess;
					if (flag2)
					{
						return operateResult2;
					}
					list.AddRange(operateResult2.Content);
					num += num2;
					bool flag3 = operateResult.Content.McDataType.DataType == 0;
					if (flag3)
					{
						operateResult.Content.AddressStart += (int)num2;
					}
					else
					{
						operateResult.Content.AddressStart += (int)(num2 * 16);
					}
				}
				result = OperateResult.CreateSuccessResult<byte[]>(list.ToArray());
			}
			return result;
		}

		// Token: 0x060008FE RID: 2302 RVA: 0x0003338C File Offset: 0x0003158C
		private OperateResult<byte[]> ReadAddressData(McAddressData addressData)
		{
			byte[] mcCore = MelsecHelper.BuildAsciiReadMcCoreCommand(addressData, false);
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(mcCore, this.NetworkNumber, this.NetworkStationNumber));
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				OperateResult operateResult2 = MelsecMcAsciiNet.CheckResponseContent(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
				}
				else
				{
					result = MelsecMcAsciiNet.ExtractActualData(operateResult.Content, false);
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x060008FF RID: 2303 RVA: 0x0003340C File Offset: 0x0003160C
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<McAddressData> operateResult = this.McAnalysisAddress(address, 0);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				byte[] mcCore = MelsecHelper.BuildAsciiWriteWordCoreCommand(operateResult.Content, value);
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(mcCore, this.NetworkNumber, this.NetworkStationNumber));
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = MelsecMcAsciiNet.CheckResponseContent(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000900 RID: 2304 RVA: 0x000334A4 File Offset: 0x000316A4
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
		{
			MelsecMcAsciiNet.<ReadAsync>d__15 <ReadAsync>d__ = new MelsecMcAsciiNet.<ReadAsync>d__15();
			<ReadAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadAsync>d__.<>4__this = this;
			<ReadAsync>d__.address = address;
			<ReadAsync>d__.length = length;
			<ReadAsync>d__.<>1__state = -1;
			<ReadAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<ReadAsync>d__15>(ref <ReadAsync>d__);
			return <ReadAsync>d__.<>t__builder.Task;
		}

		// Token: 0x06000901 RID: 2305 RVA: 0x000334F8 File Offset: 0x000316F8
		[DebuggerStepThrough]
		private Task<OperateResult<byte[]>> ReadAddressDataAsync(McAddressData addressData)
		{
			MelsecMcAsciiNet.<ReadAddressDataAsync>d__16 <ReadAddressDataAsync>d__ = new MelsecMcAsciiNet.<ReadAddressDataAsync>d__16();
			<ReadAddressDataAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadAddressDataAsync>d__.<>4__this = this;
			<ReadAddressDataAsync>d__.addressData = addressData;
			<ReadAddressDataAsync>d__.<>1__state = -1;
			<ReadAddressDataAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<ReadAddressDataAsync>d__16>(ref <ReadAddressDataAsync>d__);
			return <ReadAddressDataAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000902 RID: 2306 RVA: 0x00033544 File Offset: 0x00031744
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, byte[] value)
		{
			MelsecMcAsciiNet.<WriteAsync>d__17 <WriteAsync>d__ = new MelsecMcAsciiNet.<WriteAsync>d__17();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<WriteAsync>d__17>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.ReadRandom(System.String[])" />
		// Token: 0x06000903 RID: 2307 RVA: 0x00033598 File Offset: 0x00031798
		[HslMqttApi]
		public OperateResult<byte[]> ReadRandom(string[] address)
		{
			McAddressData[] array = new McAddressData[address.Length];
			for (int i = 0; i < address.Length; i++)
			{
				OperateResult<McAddressData> operateResult = McAddressData.ParseMelsecFrom(address[i], 1);
				bool flag = !operateResult.IsSuccess;
				if (flag)
				{
					return OperateResult.CreateFailedResult<byte[]>(operateResult);
				}
				array[i] = operateResult.Content;
			}
			byte[] mcCore = MelsecHelper.BuildAsciiReadRandomWordCommand(array);
			OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(mcCore, this.NetworkNumber, this.NetworkStationNumber));
			bool flag2 = !operateResult2.IsSuccess;
			if (flag2)
			{
				return OperateResult.CreateFailedResult<byte[]>(operateResult2);
			}
			OperateResult operateResult3 = MelsecMcAsciiNet.CheckResponseContent(operateResult2.Content);
			bool flag3 = !operateResult3.IsSuccess;
			if (flag3)
			{
				return OperateResult.CreateFailedResult<byte[]>(operateResult3);
			}
			return MelsecMcAsciiNet.ExtractActualData(operateResult2.Content, false);
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.ReadRandom(System.String[],System.UInt16[])" />
		// Token: 0x06000904 RID: 2308 RVA: 0x00033670 File Offset: 0x00031870
		public OperateResult<byte[]> ReadRandom(string[] address, ushort[] length)
		{
			bool flag = length.Length != address.Length;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = new OperateResult<byte[]>(StringResources.Language.TwoParametersLengthIsNotSame);
			}
			else
			{
				McAddressData[] array = new McAddressData[address.Length];
				for (int i = 0; i < address.Length; i++)
				{
					OperateResult<McAddressData> operateResult = McAddressData.ParseMelsecFrom(address[i], length[i]);
					bool flag2 = !operateResult.IsSuccess;
					if (flag2)
					{
						return OperateResult.CreateFailedResult<byte[]>(operateResult);
					}
					array[i] = operateResult.Content;
				}
				byte[] mcCore = MelsecHelper.BuildAsciiReadRandomCommand(array);
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(mcCore, this.NetworkNumber, this.NetworkStationNumber));
				bool flag3 = !operateResult2.IsSuccess;
				if (flag3)
				{
					result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
				}
				else
				{
					OperateResult operateResult3 = MelsecMcAsciiNet.CheckResponseContent(operateResult2.Content);
					bool flag4 = !operateResult3.IsSuccess;
					if (flag4)
					{
						result = OperateResult.CreateFailedResult<byte[]>(operateResult3);
					}
					else
					{
						result = MelsecMcAsciiNet.ExtractActualData(operateResult2.Content, false);
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.ReadRandomInt16(System.String[])" />
		// Token: 0x06000905 RID: 2309 RVA: 0x00033770 File Offset: 0x00031970
		public OperateResult<short[]> ReadRandomInt16(string[] address)
		{
			OperateResult<byte[]> operateResult = this.ReadRandom(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<short[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<short[]>(operateResult);
			}
			else
			{
				result = OperateResult.CreateSuccessResult<short[]>(base.ByteTransform.TransInt16(operateResult.Content, 0, address.Length));
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcAsciiNet.ReadRandom(System.String[])" />
		// Token: 0x06000906 RID: 2310 RVA: 0x000337BC File Offset: 0x000319BC
		[DebuggerStepThrough]
		public Task<OperateResult<byte[]>> ReadRandomAsync(string[] address)
		{
			MelsecMcAsciiNet.<ReadRandomAsync>d__21 <ReadRandomAsync>d__ = new MelsecMcAsciiNet.<ReadRandomAsync>d__21();
			<ReadRandomAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadRandomAsync>d__.<>4__this = this;
			<ReadRandomAsync>d__.address = address;
			<ReadRandomAsync>d__.<>1__state = -1;
			<ReadRandomAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<ReadRandomAsync>d__21>(ref <ReadRandomAsync>d__);
			return <ReadRandomAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcAsciiNet.ReadRandom(System.String[],System.UInt16[])" />
		// Token: 0x06000907 RID: 2311 RVA: 0x00033808 File Offset: 0x00031A08
		[DebuggerStepThrough]
		public Task<OperateResult<byte[]>> ReadRandomAsync(string[] address, ushort[] length)
		{
			MelsecMcAsciiNet.<ReadRandomAsync>d__22 <ReadRandomAsync>d__ = new MelsecMcAsciiNet.<ReadRandomAsync>d__22();
			<ReadRandomAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadRandomAsync>d__.<>4__this = this;
			<ReadRandomAsync>d__.address = address;
			<ReadRandomAsync>d__.length = length;
			<ReadRandomAsync>d__.<>1__state = -1;
			<ReadRandomAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<ReadRandomAsync>d__22>(ref <ReadRandomAsync>d__);
			return <ReadRandomAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcAsciiNet.ReadRandomInt16(System.String[])" />
		// Token: 0x06000908 RID: 2312 RVA: 0x0003385C File Offset: 0x00031A5C
		[DebuggerStepThrough]
		public Task<OperateResult<short[]>> ReadRandomInt16Async(string[] address)
		{
			MelsecMcAsciiNet.<ReadRandomInt16Async>d__23 <ReadRandomInt16Async>d__ = new MelsecMcAsciiNet.<ReadRandomInt16Async>d__23();
			<ReadRandomInt16Async>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<short[]>>.Create();
			<ReadRandomInt16Async>d__.<>4__this = this;
			<ReadRandomInt16Async>d__.address = address;
			<ReadRandomInt16Async>d__.<>1__state = -1;
			<ReadRandomInt16Async>d__.<>t__builder.Start<MelsecMcAsciiNet.<ReadRandomInt16Async>d__23>(ref <ReadRandomInt16Async>d__);
			return <ReadRandomInt16Async>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000909 RID: 2313 RVA: 0x000338A8 File Offset: 0x00031AA8
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			OperateResult<McAddressData> operateResult = this.McAnalysisAddress(address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				byte[] mcCore = MelsecHelper.BuildAsciiReadMcCoreCommand(operateResult.Content, true);
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(mcCore, this.NetworkNumber, this.NetworkStationNumber));
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<bool[]>(operateResult2);
				}
				else
				{
					OperateResult operateResult3 = MelsecMcAsciiNet.CheckResponseContent(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<bool[]>(operateResult3);
					}
					else
					{
						OperateResult<byte[]> operateResult4 = MelsecMcAsciiNet.ExtractActualData(operateResult2.Content, true);
						bool flag4 = !operateResult4.IsSuccess;
						if (flag4)
						{
							result = OperateResult.CreateFailedResult<bool[]>(operateResult4);
						}
						else
						{
							result = OperateResult.CreateSuccessResult<bool[]>(Enumerable.ToArray<bool>(Enumerable.Take<bool>(Enumerable.Select<byte, bool>(operateResult4.Content, (byte m) => m == 1), (int)length)));
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x0600090A RID: 2314 RVA: 0x000339AC File Offset: 0x00031BAC
		[HslMqttApi("WriteBoolArray", "")]
		public override OperateResult Write(string address, bool[] values)
		{
			OperateResult<McAddressData> operateResult = this.McAnalysisAddress(address, 0);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				byte[] mcCore = MelsecHelper.BuildAsciiWriteBitCoreCommand(operateResult.Content, values);
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(mcCore, this.NetworkNumber, this.NetworkStationNumber));
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = MelsecMcAsciiNet.CheckResponseContent(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x0600090B RID: 2315 RVA: 0x00033A3C File Offset: 0x00031C3C
		[DebuggerStepThrough]
		public override Task<OperateResult<bool[]>> ReadBoolAsync(string address, ushort length)
		{
			MelsecMcAsciiNet.<ReadBoolAsync>d__26 <ReadBoolAsync>d__ = new MelsecMcAsciiNet.<ReadBoolAsync>d__26();
			<ReadBoolAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
			<ReadBoolAsync>d__.<>4__this = this;
			<ReadBoolAsync>d__.address = address;
			<ReadBoolAsync>d__.length = length;
			<ReadBoolAsync>d__.<>1__state = -1;
			<ReadBoolAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<ReadBoolAsync>d__26>(ref <ReadBoolAsync>d__);
			return <ReadBoolAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x0600090C RID: 2316 RVA: 0x00033A90 File Offset: 0x00031C90
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, bool[] values)
		{
			MelsecMcAsciiNet.<WriteAsync>d__27 <WriteAsync>d__ = new MelsecMcAsciiNet.<WriteAsync>d__27();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.values = values;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<WriteAsync>d__27>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.ReadMemory(System.String,System.UInt16)" />
		// Token: 0x0600090D RID: 2317 RVA: 0x00033AE4 File Offset: 0x00031CE4
		[HslMqttApi(ApiTopic = "ReadMemory", Description = "读取缓冲寄存器的数据信息，地址直接为偏移地址")]
		public OperateResult<byte[]> ReadMemory(string address, ushort length)
		{
			bool flag = !Authorization.asdniasnfaksndiqwhawfskhfaiw();
			OperateResult<byte[]> result;
			if (flag)
			{
				result = new OperateResult<byte[]>(StringResources.Language.InsufficientPrivileges);
			}
			else
			{
				OperateResult<byte[]> operateResult = MelsecHelper.BuildAsciiReadMemoryCommand(address, length);
				bool flag2 = !operateResult.IsSuccess;
				if (flag2)
				{
					result = operateResult;
				}
				else
				{
					OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(operateResult.Content, this.NetworkNumber, this.NetworkStationNumber));
					bool flag3 = !operateResult2.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
					}
					else
					{
						OperateResult operateResult3 = MelsecMcAsciiNet.CheckResponseContent(operateResult2.Content);
						bool flag4 = !operateResult3.IsSuccess;
						if (flag4)
						{
							result = OperateResult.CreateFailedResult<byte[]>(operateResult3);
						}
						else
						{
							result = MelsecMcAsciiNet.ExtractActualData(operateResult2.Content, false);
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcAsciiNet.ReadMemory(System.String,System.UInt16)" />
		// Token: 0x0600090E RID: 2318 RVA: 0x00033BA0 File Offset: 0x00031DA0
		[DebuggerStepThrough]
		public Task<OperateResult<byte[]>> ReadMemoryAsync(string address, ushort length)
		{
			MelsecMcAsciiNet.<ReadMemoryAsync>d__29 <ReadMemoryAsync>d__ = new MelsecMcAsciiNet.<ReadMemoryAsync>d__29();
			<ReadMemoryAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadMemoryAsync>d__.<>4__this = this;
			<ReadMemoryAsync>d__.address = address;
			<ReadMemoryAsync>d__.length = length;
			<ReadMemoryAsync>d__.<>1__state = -1;
			<ReadMemoryAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<ReadMemoryAsync>d__29>(ref <ReadMemoryAsync>d__);
			return <ReadMemoryAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.ReadSmartModule(System.UInt16,System.String,System.UInt16)" />
		// Token: 0x0600090F RID: 2319 RVA: 0x00033BF4 File Offset: 0x00031DF4
		[HslMqttApi(ApiTopic = "ReadSmartModule", Description = "读取智能模块的数据信息，需要指定模块地址，偏移地址，读取的字节长度")]
		public OperateResult<byte[]> ReadSmartModule(ushort module, string address, ushort length)
		{
			bool flag = !Authorization.asdniasnfaksndiqwhawfskhfaiw();
			OperateResult<byte[]> result;
			if (flag)
			{
				result = new OperateResult<byte[]>(StringResources.Language.InsufficientPrivileges);
			}
			else
			{
				OperateResult<byte[]> operateResult = MelsecHelper.BuildAsciiReadSmartModule(module, address, length);
				bool flag2 = !operateResult.IsSuccess;
				if (flag2)
				{
					result = operateResult;
				}
				else
				{
					OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(operateResult.Content, this.NetworkNumber, this.NetworkStationNumber));
					bool flag3 = !operateResult2.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
					}
					else
					{
						OperateResult operateResult3 = MelsecMcAsciiNet.CheckResponseContent(operateResult2.Content);
						bool flag4 = !operateResult3.IsSuccess;
						if (flag4)
						{
							result = OperateResult.CreateFailedResult<byte[]>(operateResult3);
						}
						else
						{
							result = MelsecMcAsciiNet.ExtractActualData(operateResult2.Content, false);
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcAsciiNet.ReadSmartModule(System.UInt16,System.String,System.UInt16)" />
		// Token: 0x06000910 RID: 2320 RVA: 0x00033CB0 File Offset: 0x00031EB0
		[DebuggerStepThrough]
		public Task<OperateResult<byte[]>> ReadSmartModuleAsync(ushort module, string address, ushort length)
		{
			MelsecMcAsciiNet.<ReadSmartModuleAsync>d__31 <ReadSmartModuleAsync>d__ = new MelsecMcAsciiNet.<ReadSmartModuleAsync>d__31();
			<ReadSmartModuleAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadSmartModuleAsync>d__.<>4__this = this;
			<ReadSmartModuleAsync>d__.module = module;
			<ReadSmartModuleAsync>d__.address = address;
			<ReadSmartModuleAsync>d__.length = length;
			<ReadSmartModuleAsync>d__.<>1__state = -1;
			<ReadSmartModuleAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<ReadSmartModuleAsync>d__31>(ref <ReadSmartModuleAsync>d__);
			return <ReadSmartModuleAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.RemoteRun(System.Boolean)" />
		// Token: 0x06000911 RID: 2321 RVA: 0x00033D0C File Offset: 0x00031F0C
		[HslMqttApi]
		public OperateResult RemoteRun()
		{
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(Encoding.ASCII.GetBytes("1001000000010000"), this.NetworkNumber, this.NetworkStationNumber));
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = MelsecMcAsciiNet.CheckResponseContent(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = OperateResult.CreateSuccessResult();
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.RemoteStop" />
		// Token: 0x06000912 RID: 2322 RVA: 0x00033D7C File Offset: 0x00031F7C
		[HslMqttApi]
		public OperateResult RemoteStop()
		{
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(Encoding.ASCII.GetBytes("100200000001"), this.NetworkNumber, this.NetworkStationNumber));
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = MelsecMcAsciiNet.CheckResponseContent(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = OperateResult.CreateSuccessResult();
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.RemoteReset" />
		// Token: 0x06000913 RID: 2323 RVA: 0x00033DEC File Offset: 0x00031FEC
		[HslMqttApi]
		public OperateResult RemoteReset()
		{
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(Encoding.ASCII.GetBytes("100600000001"), this.NetworkNumber, this.NetworkStationNumber));
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = MelsecMcAsciiNet.CheckResponseContent(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = OperateResult.CreateSuccessResult();
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.ReadPlcType" />
		// Token: 0x06000914 RID: 2324 RVA: 0x00033E5C File Offset: 0x0003205C
		[HslMqttApi]
		public OperateResult<string> ReadPlcType()
		{
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(Encoding.ASCII.GetBytes("01010000"), this.NetworkNumber, this.NetworkStationNumber));
			bool flag = !operateResult.IsSuccess;
			OperateResult<string> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<string>(operateResult);
			}
			else
			{
				OperateResult operateResult2 = MelsecMcAsciiNet.CheckResponseContent(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<string>(operateResult2);
				}
				else
				{
					result = OperateResult.CreateSuccessResult<string>(Encoding.ASCII.GetString(operateResult.Content, 22, 16).TrimEnd(new char[0]));
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.ErrorStateReset" />
		// Token: 0x06000915 RID: 2325 RVA: 0x00033EF4 File Offset: 0x000320F4
		[HslMqttApi]
		public OperateResult ErrorStateReset()
		{
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(Encoding.ASCII.GetBytes("01010000"), this.NetworkNumber, this.NetworkStationNumber));
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = MelsecMcAsciiNet.CheckResponseContent(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = OperateResult.CreateSuccessResult();
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcAsciiNet.RemoteRun" />
		// Token: 0x06000916 RID: 2326 RVA: 0x00033F64 File Offset: 0x00032164
		[DebuggerStepThrough]
		public Task<OperateResult> RemoteRunAsync()
		{
			MelsecMcAsciiNet.<RemoteRunAsync>d__37 <RemoteRunAsync>d__ = new MelsecMcAsciiNet.<RemoteRunAsync>d__37();
			<RemoteRunAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<RemoteRunAsync>d__.<>4__this = this;
			<RemoteRunAsync>d__.<>1__state = -1;
			<RemoteRunAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<RemoteRunAsync>d__37>(ref <RemoteRunAsync>d__);
			return <RemoteRunAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcAsciiNet.RemoteStop" />
		// Token: 0x06000917 RID: 2327 RVA: 0x00033FA8 File Offset: 0x000321A8
		[DebuggerStepThrough]
		public Task<OperateResult> RemoteStopAsync()
		{
			MelsecMcAsciiNet.<RemoteStopAsync>d__38 <RemoteStopAsync>d__ = new MelsecMcAsciiNet.<RemoteStopAsync>d__38();
			<RemoteStopAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<RemoteStopAsync>d__.<>4__this = this;
			<RemoteStopAsync>d__.<>1__state = -1;
			<RemoteStopAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<RemoteStopAsync>d__38>(ref <RemoteStopAsync>d__);
			return <RemoteStopAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcAsciiNet.RemoteReset" />
		// Token: 0x06000918 RID: 2328 RVA: 0x00033FEC File Offset: 0x000321EC
		[DebuggerStepThrough]
		public Task<OperateResult> RemoteResetAsync()
		{
			MelsecMcAsciiNet.<RemoteResetAsync>d__39 <RemoteResetAsync>d__ = new MelsecMcAsciiNet.<RemoteResetAsync>d__39();
			<RemoteResetAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<RemoteResetAsync>d__.<>4__this = this;
			<RemoteResetAsync>d__.<>1__state = -1;
			<RemoteResetAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<RemoteResetAsync>d__39>(ref <RemoteResetAsync>d__);
			return <RemoteResetAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcAsciiNet.ReadPlcType" />
		// Token: 0x06000919 RID: 2329 RVA: 0x00034030 File Offset: 0x00032230
		[DebuggerStepThrough]
		public Task<OperateResult<string>> ReadPlcTypeAsync()
		{
			MelsecMcAsciiNet.<ReadPlcTypeAsync>d__40 <ReadPlcTypeAsync>d__ = new MelsecMcAsciiNet.<ReadPlcTypeAsync>d__40();
			<ReadPlcTypeAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
			<ReadPlcTypeAsync>d__.<>4__this = this;
			<ReadPlcTypeAsync>d__.<>1__state = -1;
			<ReadPlcTypeAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<ReadPlcTypeAsync>d__40>(ref <ReadPlcTypeAsync>d__);
			return <ReadPlcTypeAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcAsciiNet.ErrorStateReset" />
		// Token: 0x0600091A RID: 2330 RVA: 0x00034074 File Offset: 0x00032274
		[DebuggerStepThrough]
		public Task<OperateResult> ErrorStateResetAsync()
		{
			MelsecMcAsciiNet.<ErrorStateResetAsync>d__41 <ErrorStateResetAsync>d__ = new MelsecMcAsciiNet.<ErrorStateResetAsync>d__41();
			<ErrorStateResetAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<ErrorStateResetAsync>d__.<>4__this = this;
			<ErrorStateResetAsync>d__.<>1__state = -1;
			<ErrorStateResetAsync>d__.<>t__builder.Start<MelsecMcAsciiNet.<ErrorStateResetAsync>d__41>(ref <ErrorStateResetAsync>d__);
			return <ErrorStateResetAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x0600091B RID: 2331 RVA: 0x000340B8 File Offset: 0x000322B8
		public override string ToString()
		{
			return string.Format("MelsecMcAsciiNet[{0}:{1}]", this.IpAddress, this.Port);
		}

		/// <summary>
		/// 将MC协议的核心报文打包成一个可以直接对PLC进行发送的原始报文
		/// </summary>
		/// <param name="mcCore">MC协议的核心报文</param>
		/// <param name="networkNumber">网络号</param>
		/// <param name="networkStationNumber">网络站号</param>
		/// <returns>原始报文信息</returns>
		// Token: 0x0600091C RID: 2332 RVA: 0x000340D8 File Offset: 0x000322D8
		public static byte[] PackMcCommand(byte[] mcCore, byte networkNumber = 0, byte networkStationNumber = 0)
		{
			byte[] array = new byte[22 + mcCore.Length];
			array[0] = 53;
			array[1] = 48;
			array[2] = 48;
			array[3] = 48;
			array[4] = SoftBasic.BuildAsciiBytesFrom(networkNumber)[0];
			array[5] = SoftBasic.BuildAsciiBytesFrom(networkNumber)[1];
			array[6] = 70;
			array[7] = 70;
			array[8] = 48;
			array[9] = 51;
			array[10] = 70;
			array[11] = 70;
			array[12] = SoftBasic.BuildAsciiBytesFrom(networkStationNumber)[0];
			array[13] = SoftBasic.BuildAsciiBytesFrom(networkStationNumber)[1];
			array[14] = SoftBasic.BuildAsciiBytesFrom((ushort)(array.Length - 18))[0];
			array[15] = SoftBasic.BuildAsciiBytesFrom((ushort)(array.Length - 18))[1];
			array[16] = SoftBasic.BuildAsciiBytesFrom((ushort)(array.Length - 18))[2];
			array[17] = SoftBasic.BuildAsciiBytesFrom((ushort)(array.Length - 18))[3];
			array[18] = 48;
			array[19] = 48;
			array[20] = 49;
			array[21] = 48;
			mcCore.CopyTo(array, 22);
			return array;
		}

		/// <summary>
		/// 从PLC反馈的数据中提取出实际的数据内容，需要传入反馈数据，是否位读取
		/// </summary>
		/// <param name="response">反馈的数据内容</param>
		/// <param name="isBit">是否位读取</param>
		/// <returns>解析后的结果对象</returns>
		// Token: 0x0600091D RID: 2333 RVA: 0x000341C4 File Offset: 0x000323C4
		public static OperateResult<byte[]> ExtractActualData(byte[] response, bool isBit)
		{
			OperateResult<byte[]> result;
			if (isBit)
			{
				result = OperateResult.CreateSuccessResult<byte[]>(Enumerable.ToArray<byte>(Enumerable.Select<byte, byte>(response.RemoveBegin(22), (byte m) => (m == 48) ? 0 : 1)));
			}
			else
			{
				result = OperateResult.CreateSuccessResult<byte[]>(MelsecHelper.TransAsciiByteArrayToByteArray(response.RemoveBegin(22)));
			}
			return result;
		}

		/// <summary>
		/// 检查反馈的内容是否正确的
		/// </summary>
		/// <param name="content">MC的反馈的内容</param>
		/// <returns>是否正确</returns>
		// Token: 0x0600091E RID: 2334 RVA: 0x00034228 File Offset: 0x00032428
		public static OperateResult CheckResponseContent(byte[] content)
		{
			ushort num = Convert.ToUInt16(Encoding.ASCII.GetString(content, 18, 4), 16);
			bool flag = num > 0;
			OperateResult result;
			if (flag)
			{
				result = new OperateResult((int)num, MelsecHelper.GetErrorDescription((int)num));
			}
			else
			{
				result = OperateResult.CreateSuccessResult();
			}
			return result;
		}
	}
}
